package com.xy.smartsms.pluginxy;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.xy.bizport.service.aidl.IXyRemoteCallable;

public class XyIpcUtil {
    private static final String TAG = "XyIpcUtil";
    private static final int CONNECT_INTERVAL = 500;
    private static final int CONNECT_COUNT = 5000 / CONNECT_INTERVAL;

    private static XyIpcUtil sXyIpcUtil = null;

    private Context mContext = null;
    private IXyRemoteCallable mXyAIDLCallable;
    private String mServicePackage = "com.xy.bizport";
    private String mServiceAction = "com.xy.bizport.service.aidl.RemoteCallableStub";

    public static boolean sCanReConnection = true;

    /** SMARTISAN-707 kangwenbo 20170614 start**/
    public void unBind() {
        sCanReConnection = false;
        Log.i(TAG, "unBind:");
        try{
            if (mXyAIDLCallable != null && mContext != null ) {
                mContext.unbindService(mServiceConnection);
            }    
        }catch (Throwable t){
            Log.i(TAG, "unBind:" + t.toString());
        }
    }
    /** SMARTISAN-707 kangwenbo 20170614 end**/

    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected");
            if (sCanReConnection)
                initXyService();
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected:" + service);
            mXyAIDLCallable = IXyRemoteCallable.Stub.asInterface(service);
        }
    };

    private void initXyService() {
        mXyAIDLCallable = null;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "initXyService....");
                int count = CONNECT_COUNT;
                Intent intentSms = new Intent();
                intentSms.setPackage(mServicePackage);
                intentSms.setAction(mServiceAction);
                do {
                    Log.i(TAG, "try bind....");
                    boolean result = mContext.bindService(intentSms, mServiceConnection, Context.BIND_AUTO_CREATE);
                    Log.i(TAG, "try bind...." + result);
                    if (result) {
                        break;
                    }

                    try {
                        Thread.sleep(CONNECT_INTERVAL); // 0.5
                    }
                    catch (Throwable t) {
                        Log.e(TAG, t.getMessage());
                    }
                }
                while (count-- > 0);
            }
        }).start();

    }

    public IXyRemoteCallable getRemoteCallable() {
        return mXyAIDLCallable;
    }

    public static XyIpcUtil getInstance() {
        return sXyIpcUtil;
    }

    public static XyIpcUtil getInstance(Context context) {
        synchronized (XyIpcUtil.class) {
            if (sXyIpcUtil == null) {
                sXyIpcUtil = new XyIpcUtil(context);
            }
        }
        return sXyIpcUtil;
    }

    private XyIpcUtil(Context context) {
        mContext = context;
    }

    public void init(String servicePackage, String serviceAction) {
        if (!TextUtils.isEmpty(servicePackage)) {
            mServicePackage = servicePackage;
        }
        if (!TextUtils.isEmpty(serviceAction)) {
            mServiceAction = serviceAction;
        }
        initXyService();
    }
}